import getData from '../../service/getData2.js'
import * as types from '../types'
import * as types1 from '../appendType'

const state = {
	findList:[],
	findPage:1,
	findPageLimit:8,
	find:[],
	findListLeft:[],
	findListRight:[],
    findInfo:[],
    orderInfo:[],
    dealInfo:[],
    businessInfo:[],

    orderPage:1,
    orderPageLimit:10,
    orderPageList:[],
    scroll: true, //避免快速滚动，无限拉取数据源

    //优惠活动
    activityPage:1,
    activityPageLimit:10,
    activityPageList:[],
}
var me=this;
const actions = {

    //发现列表
	findList:function({commit,rootState},params){
        
		let page = {"aoData":"[{\"name\":\"iDisplayStart\",\"value\":"+state.findPage+"}," +
        "{\"name\":\"iDisplayLength\",\"value\":"+state.findPageLimit+"}]"
        };

        params.data = Object.assign(page,params.data);
        return new Promise((resolve, reject) => {

            if(rootState.coupon.scroll) {
                // console.log(state)
                commit(types.GET_SCORLL_STATUS,false);//避免快速滚动
                getData.findList(params).then(function (rep) {

                    commit(types.GET_SCORLL_STATUS,true);//避免快速滚动
                    resolve(rep);
                    if(rep.respCode == "00"){
                        commit(types1.FIND,rep);
                        if(rep.content.docList.length == 0) {
                            return;
                        }
                        state.findPage++;
                    }
                }).catch(function(rep){
                    reject(rep)
                });
            }
        });
	},
    //发现详情
    findDetail:function({commit},params){

        return new Promise(
            (resolve, reject) => {
                getData.findDetail(params)
                .then(rep => {
                    resolve(rep);
                    commit(types1.FIND_DETAIL,rep);
                })
                .catch(rep => {
                    reject(rep)
                })
            }
        );
    },
    //订单详情
    // orderDetail:function({commit},params){
    //     return new Promise(
    //         (resolve, reject) => {
    //             getData.orderDetail(params)
    //                 .then(rep => {
    //                     resolve(rep);
    //                     commit(types1.ORDER_DETAIL,rep);
    //                 })
    //                 .catch(rep => {
    //                     reject(rep)
    //                 })

    //         }
    //     );
    // },
    //交易详情
    dealDetail:function({commit},params){
        return new Promise(
            (resolve,reject)=>{
                getData.dealDetail(params)
                    .then(rep=>{
                        resolve(rep);
                        commit(types1.DEAL_DETAIL,rep);
                    })
                    .catch(rep=>{
                        reject(rep)
                    })
            }
        )
    },

    // 商户电子券优惠全部挂单
    businessOrder:function({commit,rootState },params){
        let page = {"aoData":"[{\"name\":\"iDisplayStart\",\"value\":"+state.activityPage+"}," +
        "{\"name\":\"iDisplayLength\",\"value\":"+state.orderPageLimit+"}]"
        };
        params.data = Object.assign(page,params.data);
        return new Promise((resolve, reject) => {
            if(rootState.coupon.scroll) {
                commit(types.GET_SCORLL_STATUS,false);//避免快速滚动
                getData2.businessOrder(params).then(function (rep) {
                    commit(types.GET_SCORLL_STATUS,true);//避免快速滚动
                    resolve(rep);
                    if(rep.respCode == "00"){
                        commit(types1.ORDER,rep);
                        if(rep.content.couponSellList.length == 0) {
                            return;
                        }
                        state.orderPage++;
                    }
                }).catch(function(rep){
                    reject(rep)
                });
            }
        });
    },
    // 商户优惠活动
    businessActivity:function({commit,rootState },params){
        let page = {"aoData":"[{\"name\":\"iDisplayStart\",\"value\":"+state.activityPage+"}," +
        "{\"name\":\"iDisplayLength\",\"value\":"+state.activityPageLimit+"}]"
        };
        params.data = Object.assign(page,params.data);
        return new Promise((resolve, reject) => {
            if(rootState.coupon.scroll) {
                commit(types.GET_SCORLL_STATUS,false);//避免快速滚动
                getData.getShopActivity(params).then(function (rep) {
                    commit(types.GET_SCORLL_STATUS,true);//避免快速滚动

                    resolve(rep);
                    if(rep.respCode == "00"){
                        commit(types1.ACTIVITY,rep);
                        if(rep.content.list.length == 0) {
                            return;
                        }
                        state.activityPage++;
                    }
                }).catch(function(rep){
                    reject(rep)
                });
            }
        });
    },
}
const getters = {
    //userId({state, getters, rootState}){
    //    return rootState.user.userInfo.userId;
    //}
}
const mutations = {
	//列表
    [types1.FIND](state,rep){
        if(!rep){
            state.findList = [];
            state.findListLeft = [];
            state.findListRight = [];
            return
        }
        // for (var i = rep.content.docList.length - 1; i >= 0; i--) {
        //     rep.content.docList[i].crtTime=new Date(rep.content.docList[i].crtTime).format('Y年MM月dd日');
        // };
        if (state.findPage <= 1) {
            state.findList = rep.content.docList;
        } else {
            state.findList = state.findList.concat(rep.content.docList);
            
        }

        for (var i = 0; i <state.findList.length/2; i++) {
            state.findListLeft[i]=state.findList[i*2];
            if (state.findList[i*2+1]!=undefined) {
                state.findListRight[i]=state.findList[i*2+1];
            }
        }
    },
    //分页
    [types1.FIND_PAGE](state,num){
        state.findPage = num;
    },
    // 详情
    [types1.FIND_DETAIL](state,rep){
        // rep.content.markeList[0].crtTime=new Date(rep.content.markeList[0].crtTime).format('Y年MM月dd日');
        state.findInfo=rep.content.markeList;
    },
    // 订单详情
    [types1.ORDER_DETAIL](state,rep){
        state.orderInfo=rep.content;
    },
    //交易详情
    [types1.DEAL_DETAIL](state,rep){
        state.dealInfo=rep.content.mcht;
    },
    //商户电子券优惠挂单列表
    [types1.ORDER](state,rep){
        if(!rep){
            state.orderPageList = [];
            return
        }
        if (state.orderPage <= 1) {
            state.orderPageList = rep.content.couponSellList;
        } else {
            state.orderPageList = state.orderPageList.concat(rep.content.couponSellList);           
        }
    },
    //商户电子券优惠挂单分页
    [types1.ORDER_PAGE](state,num){
        state.orderPage = num;
    },

    //商户优惠活动列表
    [types1.ACTIVITY](state,rep){
        if(!rep){
            state.activityPageList = [];
            return
        }
        if (state.activityPage <= 1) {
            state.activityPageList = rep.content.list;
        } else {
            state.activityPageList = state.activityPageList.concat(rep.content.list);           
        }
    },
    //商户优惠活动分页
    [types1.ACTIVITY_PAGE](state,num){
        state.activityPage = num;
    },
    
}

export default {
    state,
    actions,
    getters,
    mutations
}